package com.gitbl.webchat.server;

import com.gitbl.webchat.db.BeanConver;
import com.gitbl.webchat.db.DbUtils;
import com.gitbl.webchat.db.bean.UserBean;
import com.gitbl.webchat.db.mapper.UserMapper;
import com.gitbl.webchat.message.User;
import com.gitbl.webchat.service.UserService;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
//import org.apache.dubbo.config.annotation.Service;


public class UserServer implements UserService {
    DbUtils db;

    void initDb(){
        //InputStream is = Application.class.getClassLoader().getResourceAsStream("db.xml");

        db = new DbUtils("db.xml");

    }

    public UserServer(){
        initDb();

    }
    public UserServer(DbUtils dbu){
        db = dbu;
    }
    public boolean exist(String s) {
        return db.getSingleReader(UserMapper.class).exist(s);
    }

    public User login(String usr, String pwd) {
        UserBean b = db.getSingleReader(UserMapper.class).login(usr, DbUtils.getMD5(pwd));
        if(b==null){
            return null;
        }
        return BeanConver.Convert(b);
    }

    public boolean signup(User user) {
        try {
            return  db.getSingleWriter(UserMapper.class).signup(BeanConver.Convert(user));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
